{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\"\"\"A very simple MNIST classifier.\n",
    "See extensive documentation at https://www.tensorflow.org/get_started/mnist/beginners\n",
    "\"\"\"\n",
    "\n",
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们在这里调用系统提供的 MNIST 数据函数为我们读入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting /tmp/tensorflow/mnist/input_data/train-images-idx3-ubyte.gz\n",
      "Extracting /tmp/tensorflow/mnist/input_data/train-labels-idx1-ubyte.gz\n",
      "Extracting /tmp/tensorflow/mnist/input_data/t10k-images-idx3-ubyte.gz\n",
      "Extracting /tmp/tensorflow/mnist/input_data/t10k-labels-idx1-ubyte.gz\n"
     ]
    }
   ],
   "source": [
    "# Import data\n",
    "data_dir = '/tmp/tensorflow/mnist/input_data'\n",
    "mnist = input_data.read_data_sets(data_dir, one_hot=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(55000, 10)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mnist.train.labels.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "设置batch_size的大小为300"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "batch_size = 300\n",
    "n_batch = mnist.train.num_examples // batch_size  #所有数据集上的数据完成一次训练多少个batch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#定义输入与真实值\n",
    "x = tf.placeholder(tf.float32, [None, 784])\n",
    "y = tf.placeholder(tf.float32, [None, 10])\n",
    "keep_prob = tf.placeholder(tf.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#创建一个的神经网络  \n",
    "#输入层784，隐藏层一1600，隐藏层二1200，输出层10个神经元 ，在每一层网络最后加入dropout防止过拟合\n",
    "#.truncated_normal作初始化\n",
    "W1 = tf.Variable(tf.truncated_normal([784, 1600], stddev=0.1))  \n",
    "b1 = tf.Variable(tf.zeros([1600]) + 0.1)  \n",
    "L1 = tf.nn.tanh(tf.matmul(x, W1) + b1)  \n",
    "L1_drop = tf.nn.dropout(L1,keep_prob)  \n",
    "  \n",
    "  \n",
    "W2 = tf.Variable(tf.truncated_normal([1600, 1200], stddev=0.1))  \n",
    "b2 = tf.Variable(tf.zeros([1200]) + 0.1)  \n",
    "L2 = tf.nn.tanh(tf.matmul(L1_drop, W2) + b2)  \n",
    "L2_drop = tf.nn.dropout(L2,keep_prob)  \n",
    "  \n",
    "  \n",
    "  \n",
    "W3 = tf.Variable(tf.truncated_normal([1200, 10], stddev=0.1))  \n",
    "b3 = tf.Variable(tf.zeros([10]) + 0.1)  \n",
    "out_layer = tf.matmul(L2_drop, W3) + b3\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#添加正则项，正则参数为0.00001\n",
    "regularizer = tf.contrib.layers.l2_regularizer(0.00001 )\n",
    "regularization = regularizer(W1) + regularizer(b1) + regularizer(W2) + regularizer(b2)+ regularizer(W3)+ regularizer(b3)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#添加损失函数，用ADAM法做优化训练，学习率为0.001\n",
    "loss_all = tf.nn.softmax_cross_entropy_with_logits(logits=out_layer, labels=y, name='cross_entropy_loss')\n",
    "cross_entropy_mean = tf.reduce_mean(loss_all, name='avg_loss')\n",
    "loss = cross_entropy_mean # + regularization\n",
    "optimizer = tf.train.AdamOptimizer(0.001).minimize(loss)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#生成一个训练step\n",
    "sess = tf.Session()\n",
    "init_op = tf.global_variables_initializer()\n",
    "sess.run(init_op)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "correct_prediction = tf.equal(tf.argmax(out_layer, 1), tf.argmax(y, 1))\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Iter0, Testing Accuracy0.9392\n",
      "Iter1, Testing Accuracy0.9525\n",
      "Iter2, Testing Accuracy0.9584\n",
      "Iter3, Testing Accuracy0.963\n",
      "Iter4, Testing Accuracy0.9689\n",
      "Iter5, Testing Accuracy0.9698\n",
      "Iter6, Testing Accuracy0.9729\n",
      "Iter7, Testing Accuracy0.9721\n",
      "Iter8, Testing Accuracy0.9748\n",
      "Iter9, Testing Accuracy0.9746\n",
      "Iter10, Testing Accuracy0.9762\n",
      "Iter11, Testing Accuracy0.9753\n",
      "Iter12, Testing Accuracy0.9751\n",
      "Iter13, Testing Accuracy0.9777\n",
      "Iter14, Testing Accuracy0.9792\n",
      "Iter15, Testing Accuracy0.9772\n",
      "Iter16, Testing Accuracy0.9784\n",
      "Iter17, Testing Accuracy0.9788\n",
      "Iter18, Testing Accuracy0.9787\n",
      "Iter19, Testing Accuracy0.9806\n",
      "Iter20, Testing Accuracy0.9795\n",
      "Iter21, Testing Accuracy0.9794\n",
      "Iter22, Testing Accuracy0.98\n",
      "Iter23, Testing Accuracy0.98\n",
      "Iter24, Testing Accuracy0.9793\n",
      "Iter25, Testing Accuracy0.9807\n",
      "Iter26, Testing Accuracy0.9807\n"
     ]
    }
   ],
   "source": [
    "#设置迭代次数为30次\n",
    "for epoch in range(30):  \n",
    "    for batch in range(n_batch): \n",
    "            batch_xs, batch_ys = mnist.train.next_batch(batch_size)  \n",
    "            sess.run(optimizer, feed_dict={x:batch_xs, y:batch_ys, keep_prob:0.5})  \n",
    "        #训练完一个周期后测试数据准确率  \n",
    "    acc = sess.run(accuracy, feed_dict={x:mnist.test.images, y:mnist.test.labels, keep_prob:1.0})  \n",
    "    print(\"Iter\" + str(epoch) + \", Testing Accuracy\" + str(acc)) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "验证我们模型在测试数据上的准确率，可以看出在迭代到26次以后，准确率可以达到98%以上"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
